package com.nf.health.app.healthstate.adapter;

import android.app.Activity;
import android.os.SystemClock;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.adapter.ItemExceptionDataAdapter;
import com.nf.health.app.adapter.ItemHealthStateAdapter;
import com.nf.health.app.adapter.ItemHealthyDoctorAdapter;
import com.nf.health.app.customview.HpCircleGradeProgress;
import com.nf.health.app.models.HealthStateInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 健康状态适配器
 * Created by Jason on 2016/3/31.
 */
public class HealthStateAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int HEALTH_STATE_PROGRESS = 0;
    private static final int ITEM = 1;

    private Activity act;
    private HealthStateInfo healthStateInfo;

    private List<HealthStateInfo.ExceptionValue> exceptionValues = new ArrayList<>();


    public HealthStateAdapter(Activity activity, HealthStateInfo healthStateInfo) {
        this.act = activity;
        this.healthStateInfo = healthStateInfo;
        //解析所有异常数据



        if (healthStateInfo != null) {
            for (Map.Entry<String, HealthStateInfo.ExceptionValue> entry : healthStateInfo.getBalance_exce().entrySet()) {//体质称
                exceptionValues.add(entry.getValue());
            }
            for (Map.Entry<String, HealthStateInfo.ExceptionValue> entry : healthStateInfo.getBlood_exce().entrySet()) {//血压
                exceptionValues.add(entry.getValue());
            }
            for (Map.Entry<String, HealthStateInfo.ExceptionValue> entry : healthStateInfo.getBloodsugar_exce().entrySet()) {//血糖
                exceptionValues.add(entry.getValue());

            }
            for (Map.Entry<String, HealthStateInfo.ExceptionValue> entry : healthStateInfo.getUrine_exce().entrySet()) {//尿检
                exceptionValues.add(entry.getValue());

            }
            for (Map.Entry<String, HealthStateInfo.ExceptionValue> entry : healthStateInfo.getTemperature().entrySet()) {//体温
                exceptionValues.add(entry.getValue());

            }
        }
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (HEALTH_STATE_PROGRESS == viewType) {
            View viewHeader = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_healthy_state_header, parent, false);
            return new HeaderViewHolder(viewHeader);
        } else if (ITEM == viewType) {
            View viewHeader = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_healthy_state_content, parent, false);
            return new ItemViewHolder(viewHeader);
        }
        return null;
    }
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HeaderViewHolder) {
            setProgress(((HeaderViewHolder) holder).ivCircleProgress,healthStateInfo.getScore());//设置健康分数,动画
            ((HeaderViewHolder) holder).ivCircleProgress.setProgress(healthStateInfo.getScore());
            ((HeaderViewHolder) holder).tvPercent.setText(healthStateInfo.getScore() + "%");
            ((HeaderViewHolder) holder).tvAbnormal.setText(healthStateInfo.getFail()+"项指标存在异常");
            ((HeaderViewHolder) holder).health_text.setText(healthStateInfo.getHealth());
        } else if (holder instanceof ItemViewHolder) {
            if (position == 1) {
                ItemViewHolder itemHolder = (ItemViewHolder) holder;
                if(exceptionValues.size() == 0){
                    itemHolder.recyclerView.setVisibility(View.GONE);
                    itemHolder.all_lin_state.setVisibility(View.GONE);
                }else{
                    itemHolder.tvTitle.setText("异常数据");
                    itemHolder.recyclerView.setAdapter(new ItemExceptionDataAdapter(exceptionValues,act));
                }

            } else if (position == 2) {
                ItemViewHolder itemHolder = (ItemViewHolder) holder;
                if(healthStateInfo.getAnalysis().size() == 0){
                    itemHolder.recyclerView.setVisibility(View.GONE);
                    itemHolder.all_lin_state.setVisibility(View.GONE);
                }else{
                    itemHolder.tvTitle.setText("分析报告");
                    itemHolder.recyclerView.setAdapter(new ItemHealthStateAdapter(healthStateInfo.getAnalysis()));
                }
            } else if (position == 3) {
                ItemViewHolder itemHolder = (ItemViewHolder) holder;
                if(healthStateInfo.getBetter().size() == 0){
                    itemHolder.recyclerView.setVisibility(View.GONE);
                    itemHolder.all_lin_state.setVisibility(View.GONE);
                }else{
                    itemHolder.tvTitle.setText("改善方法");
                    itemHolder.recyclerView.setAdapter(new ItemHealthStateAdapter(healthStateInfo.getBetter()));
                }
            } else if (position == 4) {
                ItemViewHolder itemHolder = (ItemViewHolder) holder;
                if(healthStateInfo.getScheme_list().size() == 0){
                    itemHolder.recyclerView.setVisibility(View.GONE);
                    itemHolder.all_lin_state.setVisibility(View.GONE);
                }else{
                    itemHolder.tvTitle.setText("推荐方案");
                    itemHolder.recyclerView.setAdapter(new ItemSchemeAdapter(healthStateInfo.getScheme_list(), act));
                }

            }else if (position == 5) {
                ItemViewHolder itemHolder = (ItemViewHolder) holder;
                itemHolder.tvTitle.setText("推荐医生");
                // itemHolder.recyclerView.addItemDecoration(new MyDecoration(act, LinearLayoutManager.VERTICAL) );
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(act);
                linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                itemHolder.recyclerView.setLayoutManager(linearLayoutManager);
                itemHolder.recyclerView.setItemAnimator(new DefaultItemAnimator());

                if(healthStateInfo.getDocotr_list().size()==0){
                    itemHolder.recyclerView.setVisibility(View.GONE);
                    itemHolder.all_lin_state.setVisibility(View.GONE);
                }else{
                    itemHolder.recyclerView.setAdapter(new ItemHealthyDoctorAdapter(healthStateInfo.getDocotr_list(), act));
                }

            }
        }
    }
    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            //健康状态进度条
            return HEALTH_STATE_PROGRESS;
        } else {
            return ITEM;
        }
    }

    @Override
    public int getItemCount() {
        if (healthStateInfo == null) {
            return 0;
        } else {
            return 6;
        }
    }

    class HeaderViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.iv_circle_progress)
        HpCircleGradeProgress ivCircleProgress;
        @Bind(R.id.tv_health_percent)
        TextView tvPercent;
        @Bind(R.id.tv_abnormal)
        TextView tvAbnormal;
        @Bind(R.id.health_text)
        TextView health_text;

        public HeaderViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_title)
        TextView tvTitle;
        @Bind(R.id.recyclerView)
        RecyclerView recyclerView;
        @Bind(R.id.all_lin_state)
        LinearLayout all_lin_state;

        public ItemViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            recyclerView.setHasFixedSize(true);
            LinearLayoutManager mLayoutManager = new LinearLayoutManager(act);
            recyclerView.setLayoutManager(mLayoutManager);
            recyclerView.setItemAnimator(new DefaultItemAnimator());
        }
    }

    /**
     * 设置progress 用子线程完成动画效果
     *
     * @param view
     * @param target
     */
    private void setProgress(final HpCircleGradeProgress view, final float target) {
        new Thread(new Runnable() {

            @Override
            public void run() {
                SystemClock.sleep(500);
                int progress = 0;
                while (progress <= target) {
                    view.setProgress(progress);
                    progress++;
                    SystemClock.sleep(10);
                }
            }
        }).start();
    }
}